import { BaseHttpRequest, CancelablePromise, MonitorApiClient } from "@/api";
import { ApiRequestOptions } from "../types";
import { request } from "./request";

const originalProto = XMLHttpRequest.prototype;
const originalOpen = originalProto.open;
const originalSend = originalProto.send;

class OriginHttpRequest extends BaseHttpRequest {
  request<T>(options: ApiRequestOptions): CancelablePromise<T> {
    return request(this.config, options, originalOpen, originalSend);
  }
}

export const client = new MonitorApiClient({}, OriginHttpRequest);

let {
  postBehaviorsBasicbehaviors,
  postBehaviorsClickbehaviors,
  postBehaviorsPageskipbehaviors,
  postBehaviorsRoutingskipbehaviors,
  postErrorsJavascripterrors,
  postErrorsPromiseerrors,
  postErrorsResourceerrors,
  postPerformancesBasicindicators,
  postPerformancesInterfaceindicators,
  postPerformancesResourceindicators,
  postReplayReport,
} = client.service;

postBehaviorsBasicbehaviors = postBehaviorsBasicbehaviors.bind(client.service);
postBehaviorsClickbehaviors = postBehaviorsClickbehaviors.bind(client.service);
postBehaviorsPageskipbehaviors = postBehaviorsPageskipbehaviors.bind(
  client.service
);
postBehaviorsRoutingskipbehaviors = postBehaviorsRoutingskipbehaviors.bind(
  client.service
);
postErrorsJavascripterrors = postErrorsJavascripterrors.bind(client.service);
postErrorsPromiseerrors = postErrorsPromiseerrors.bind(client.service);
postErrorsResourceerrors = postErrorsResourceerrors.bind(client.service);
postPerformancesBasicindicators = postPerformancesBasicindicators.bind(
  client.service
);
postPerformancesInterfaceindicators = postPerformancesInterfaceindicators.bind(
  client.service
);
postPerformancesResourceindicators = postPerformancesResourceindicators.bind(
  client.service
);
postReplayReport = postReplayReport.bind(client.service);

export * from "@/api";
export {
  postBehaviorsBasicbehaviors,
  postBehaviorsClickbehaviors,
  postBehaviorsPageskipbehaviors,
  postBehaviorsRoutingskipbehaviors,
  postErrorsJavascripterrors,
  postErrorsPromiseerrors,
  postErrorsResourceerrors,
  postPerformancesBasicindicators,
  postPerformancesInterfaceindicators,
  postPerformancesResourceindicators,
  postReplayReport,
};
